package com.woniuxy.microsystemsarserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microsystemsarserver.bean.PageRequestBean;
import com.woniuxy.microsystemsarserver.bean.ServiceBean;
import com.woniuxy.microsystemsarserver.bean.ServiceOrderBean;
import com.woniuxy.microsystemsarserver.mapper.ServiceOrderMapper;
import com.woniuxy.microsystemsarserver.service.IServiceOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 服务订单管理业务层实现类
 * @author 杨灿
 */
@Service
@Transactional(readOnly = true)
public class ServiceOrderServiceImpl implements IServiceOrderService {
    @Resource
    private ServiceOrderMapper serviceOrderMapper;

    @Override
    public PageInfo<ServiceOrderBean> findServiceOrderByPage(ServiceOrderBean serviceOrderBean, PageRequestBean pageRequestBean,Long userId) {
        return PageHelper.startPage(pageRequestBean.getPageNum(),pageRequestBean.getLimit()).
                doSelectPageInfo(()->serviceOrderMapper.findServiceOrderByPage(serviceOrderBean,pageRequestBean,userId));
    }

    @Override
    public List<ServiceOrderBean> findAllServiceOrder(Long userId) {

        return serviceOrderMapper.findAllServiceOrder(userId);
    }

    @Override
    public ServiceOrderBean findServiceOrderById(Long serviceOrderId) {

        return serviceOrderMapper.findServiceOrderById(serviceOrderId);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int updateServiceOrder(ServiceOrderBean serviceOrderBean) {

        return serviceOrderMapper.updateServiceOrder(serviceOrderBean);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int saveServiceOrder(ServiceOrderBean serviceOrderBean) {
        //生成订单号
        long time = new Date().getTime();
        serviceOrderBean.setOrderNum(time+"");
        //设置状态
        serviceOrderBean.setOrderStatus((byte) 0);
        return serviceOrderMapper.saveServiceOrder(serviceOrderBean);
    }

    @Override
    public List<ServiceBean> findAllService() {

        return serviceOrderMapper.findAllService();
    }

    @Override
    public int countByUserId(Long userId) {
        return serviceOrderMapper.countByUserId(userId);
    }
}
